/*******************************************************************************
* Project: QSAR(Cute Software Architecture) 
*
* Copyright: Copyright(C) 2024 by YuetingBen, All Rights Reserved
*
* File name: DcmType.h
*
* Brief: ;
*
* Author: Yueting.Ben
*
* Module: Dcm
*
*******************************************************************************/
#ifndef DCMTYPE_H
#define DCMTYPE_H

/** HEADER FILES **/
#include "PlatformType.h"
#include "PlatformCfg.h"
#include "ComStackType.h"

/** MACRO DEFINITION **/
#define DCM_DIAG_DATA_BUFFER_MAX_SIZE                            100

#define DCM_SDU_SID_INDEX           0u
#define DCM_SDU_SUBID_INDEX         1u


#define DCM_NRC_11_SERVICE_NOT_SUP     0x11 /* serviceNotSupported */
#define DCM_NRC_12_SUB_FUNC_NOT_SUP    0x12 /* subFunctionNotSupported */
#define DCM_NRC_13_INCORRECT_MSG_LEN_FORMAT   0x13 /* incorrectMessageLengthOrInvalidFormat */
#define DCM_NRC_22_CONDITIONS_NOT_CORRECT    0x22/* conditionsNotCorrect */
#define DCM_NRC_31_REQUEST_OUT_OF_RANGE      0x31 /* requestOutOfRange */
#define DCM_NRC_33_SECURITY_ACCESS_DENIED    0x33 /* securityAccessDenied */
#define DCM_NRC_35_INVALID_KEY_DENIED        0x35 /* invalidKey */

#define DCM_NRC_7E_SUB_FUNC_NOT_SUPPORT_IN_ACTIVE_SESSION   0x7E /* subFunctionNotSupportedInActiveSession */
#define DCM_NRC_7F_SERVICE_NOT_SUPPORT_IN_ACTIVE_SESSION    0x7F /* serviceNotSupportedInActiveSession */



/** TYPEDEF DEFINITION **/


/* Base type for diagnostic message item */
typedef uint8 Dcm_MsgItemType;

/* Base type for diagnostic message (request, positive or negative response) */
typedef Dcm_MsgItemType* Dcm_MsgType;

typedef uint32 Dcm_MsgLenType;

/* This index for diagnostic request and response data buffer */
typedef uint32 Dcm_DataBufferIndexType;

/* This message context identifier can be used to determine the relation between request and response confirmation */
typedef uint8 Dcm_IdContextType;


typedef struct
{
    uint8 reqType                :1; /*0 - physical request, 1 -  functional request */
    uint8 suppressPosResponse   :1; /* 0 - no (do not suppress, 1 -  yes (no positive response will be sent) */
}Dcm_MsgAddInfoType;


typedef uint8 Dcm_SecLevelType;
#define DCM_SEC_LEV_LOCKED ((Dcm_SecLevelType)0x00u)
#define DCM_SEC_LEV_1_UNLOCKED ((Dcm_SecLevelType)0x01u)
#define DCM_SEC_LEV_2_UNLOCKED ((Dcm_SecLevelType)0x02u)
#define DCM_SEC_LEV_MAX 0x03u

typedef uint8 Dcm_SesCtrlType;
#define DCM_DEFAULT_SESSION ((Dcm_SesCtrlType) 0x01u)
#define DCM_PROGRAMMING_SESSION  ((Dcm_SesCtrlType) 0x02u)
#define DCM_EXTENDED_DIAGNOSTIC_SESSION ((Dcm_SesCtrlType) 0x03u)
#define DCM_SUPPLIER_SESSION ((Dcm_SesCtrlType) 0x60u)
#define DCM_SESSION_MAX 0x04u

typedef uint8 Dcm_ProgressType;
#define DCM_PROGRESS_INITIAL ((Dcm_ProgressType)0x00u)
#define DCM_PROGRESS_LENGTH_FORMAT_CHECK ((Dcm_ProgressType)0x01u)
#define DCM_PROGRESS_IMPLEMENTATION ((Dcm_ProgressType)0x02u)


typedef struct
{
    Dcm_MsgType reqData; /* Request data, starting directly after service identifier (which is not part of this data) */
    Dcm_MsgLenType reqDataLen; /* Request data length (excluding service identifier */
    Dcm_DataBufferIndexType reqDataIndex;
    Dcm_MsgType resData; /* Positive response data, starting directly after service identifier (which is not part of this data) */
    Dcm_MsgLenType resDataLen; /* Positive response data length (excluding service identifier */
    Dcm_DataBufferIndexType resDataIndex;
    Dcm_MsgAddInfoType msgAddInfo; /* Additional information about service request and response */
    Dcm_MsgLenType resMaxDataLen; /* The maximal length of a response is restricted by the size of the buffer */
    Dcm_IdContextType idContext; /* This message context identifier can be used to determine the relation between request and response confirmation */
    PduIdType dcmRxPduId; /* Pdu identifier on which the request was received */
    Dcm_ProgressType* progressPtr;
}Dcm_MsgContextType;


typedef struct
{
    Dcm_MsgContextType diagContex; /* Diagnostic contex */
    uint8 indicationFlag;
    Dcm_MsgType sdu;
    Dcm_MsgItemType negResBuffer[3];
    Dcm_MsgType pdu;
    Dcm_DataBufferIndexType sduDataIndex;
    Dcm_MsgLenType sduLength; /* Positive response data length (excluding service identifier */
    Dcm_ProgressType progress;
}Dcm_InfoType;


typedef uint8 Dcm_ExtendedOpStatusType;
#define DCM_INITIAL ((Dcm_ExtendedOpStatusType)0x00u)
#define DCM_PENDING ((Dcm_ExtendedOpStatusType)0x01u)
#define DCM_CANCEL ((Dcm_ExtendedOpStatusType)0x02u)
#define DCM_FORCE_RCRRP_OK ((Dcm_ExtendedOpStatusType)0x03u)
#define DCM_POS_RESPONSE_SENT ((Dcm_ExtendedOpStatusType)0x04u)
#define DCM_POS_RESPONSE_FAILED ((Dcm_ExtendedOpStatusType)0x05u)
#define DCM_NEG_RESPONSE_SENT ((Dcm_ExtendedOpStatusType)0x06u)
#define DCM_NEG_RESPONSE_FAILED ((Dcm_ExtendedOpStatusType)0x07u)


/* For the allowed NRC of the executed Service ID please refer to the specification of the service in ISO14229-1 (UDS) and ISO15031-5 (OBD/CARB) */
typedef uint8 Dcm_NegativeResponseCodeType;
#define DCM_POS_RESP (Dcm_NegativeResponseCodeType)0x00u
#define DCM_E_GENERALREJECT (Dcm_NegativeResponseCodeType)0x10u
#define DCM_E_SERVICENOTSUPPORTED (Dcm_NegativeResponseCodeType)0x11u
#define DCM_E_SUBFUNCTION_NOTSUPPORTE (Dcm_NegativeResponseCodeType)0x12u
#define DCM_E_INCORRECTMES_SAGELENGTHORINVALID_FORMAT (Dcm_NegativeResponseCodeType)0x13u
#define DCM_E_RESPONSETOOLONG (Dcm_NegativeResponseCodeType)0x14u
#define DCM_E_BUSYREPEATREQUEST (Dcm_NegativeResponseCodeType)0x21u
#define DCM_E_REQUESTOUTOFRANGE (Dcm_NegativeResponseCodeType)0x31u
#define DCM_E_SECURITYACCESSDENIED (Dcm_NegativeResponseCodeType)0x33u
#define DCM_E_INVALIDKEY (Dcm_NegativeResponseCodeType)0x35u
#define DCM_E_EXCEEDNUMBEROFATTEMPTS (Dcm_NegativeResponseCodeType)0x36u
#define DCM_E_REQUIREDTIMEDELAYNOTEXPIRED (Dcm_NegativeResponseCodeType)0x37u
#define DCM_E_SUBFUNCTIONNOTSUPPORTEDINACTIVESESSION (Dcm_NegativeResponseCodeType)0x7Eu
#define DCM_E_SERVICENOTSUPPORTEDINACTIVESESSION (Dcm_NegativeResponseCodeType)0x7Fu

typedef uint8 Dcm_OpStatusType;


typedef Std_ReturnType (*Dcm_ProcessorFncType)(
    Dcm_ExtendedOpStatusType OpStatus,
    Dcm_MsgContextType* pMsgContext, 
    Dcm_NegativeResponseCodeType* ErrorCode
);


typedef Std_ReturnType (*Dcm_ReadDataFncType)(
    uint8* Data
);

typedef Std_ReturnType (*Dcm_WriteDataFncType)(
    uint8* Data
);

typedef Std_ReturnType (*Dcm_ReturnControlToECU)(
    uint8* controlMask,
    Dcm_NegativeResponseCodeType* ErrorCode
);

typedef Std_ReturnType (*Dcm_ResetToDefault)(
    uint8* controlMask,
    Dcm_NegativeResponseCodeType* ErrorCode
);

typedef Std_ReturnType (*Dcm_FreezeCurrentState)(
    uint8* controlMask,
    Dcm_NegativeResponseCodeType* ErrorCode
);

typedef Std_ReturnType (*Dcm_ShortTermAdjustment)(
    const uint8* ControlStateInfo,
    uint8* controlMask,
    Dcm_NegativeResponseCodeType* ErrorCode
);


typedef Std_ReturnType (*Dcm_SecurityGetSeedFncType)(
    Dcm_OpStatusType OpStatus,
    uint8* Seed,
    Dcm_NegativeResponseCodeType* ErrorCode
);

typedef Std_ReturnType (*Dcm_SecurityCompareKeyFncType)(
    const uint8* Key,
    Dcm_OpStatusType OpStatus,
    Dcm_NegativeResponseCodeType* ErrorCode
);


#define DCM_E_PENDING (Std_ReturnType)0x02u
#define DCM_E_FORCE_RCRRP (Std_ReturnType)0x03u

#define DCM_NULL_PROCESSOR (NULL_PTR)



#endif

